package com.cxyd.modules.wms.excel.listener;

import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.cxyd.common.exception.RootException;
import com.cxyd.modules.wms.dao.PurchaseDao;
import com.cxyd.modules.wms.dao.PurchaseDetailDao;
import com.cxyd.modules.wms.dto.PurchaseDTO;
import com.cxyd.modules.wms.dto.PurchaseDetailDTO;
import com.cxyd.modules.wms.dto.SupplierDTO;
import com.cxyd.modules.wms.entity.PurchaseEntity;
import com.cxyd.modules.wms.entity.SpuEntity;
import com.cxyd.modules.wms.excel.easy.PurchaseEasyExcel;
import com.cxyd.modules.wms.service.PurchaseDetailService;
import com.cxyd.modules.wms.service.PurchaseService;
import com.cxyd.modules.wms.service.SpuService;
import com.cxyd.modules.wms.service.SupplierService;
import org.apache.commons.lang3.ObjectUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

public class PurchaseExcelListener extends AnalysisEventListener<PurchaseEasyExcel> {

    private static final Logger logger = LoggerFactory.getLogger(PurchaseExcelListener.class);

    private final PurchaseDao purchaseDao;
    private final PurchaseService purchaseService;
    private final PurchaseDetailDao purchaseDetailDao;
    private final PurchaseDetailService purchaseDetailService;
    private final SpuService spuService;
    private final SupplierService supplierService;

    private static final int BATCH_COUNT = 1;
    private List<PurchaseEasyExcel> cache = new ArrayList<>(BATCH_COUNT);
    private static final String COMMENT = "入库单批量导入";
    private int total = 0;
    private PurchaseDTO purchase = new PurchaseDTO();

    public PurchaseExcelListener(PurchaseDao purchaseDao, PurchaseService purchaseService, PurchaseDetailDao purchaseDetailDao, PurchaseDetailService purchaseDetailService, SpuService spuService, SupplierService supplierService) {
        this.purchaseDao = purchaseDao;
        this.purchaseService = purchaseService;
        this.purchaseDetailDao = purchaseDetailDao;
        this.purchaseDetailService = purchaseDetailService;
        this.spuService = spuService;
        this.supplierService = supplierService;
    }

    @Override
    public void invoke(PurchaseEasyExcel purchaseEasyExcel, AnalysisContext analysisContext) {
        cache.add(purchaseEasyExcel);
        if (total == 0) {
            savePurchase();
        }
        if (cache.size() >= BATCH_COUNT) {
            saveDetail();
            cache.clear();
        }
        total++;
    }

private void savePurchase() {
    PurchaseEasyExcel purchaseEasyExcel = cache.get(0);
    if (StrUtil.isEmpty(purchaseEasyExcel.getDeliverNo())) {
        throw new RootException("收货单号不可为空！");
    }
    if (StrUtil.isEmpty(purchaseEasyExcel.getSupplierCode())) {
        throw new RootException("生产单位不可为空！");
    }
    PurchaseDTO insertPurchase = new PurchaseDTO();
    String supplierCode = purchaseEasyExcel.getSupplierCode();
    insertPurchase.setDeliverNo(purchaseEasyExcel.getDeliverNo());
    SupplierDTO supplierDTO = supplierService.getSupplierByCode(supplierCode);
    if (ObjectUtils.isEmpty(supplierDTO)) {
        throw new RootException("请添加单位资料：" + supplierCode);
    }
    insertPurchase.setSupplierCode(supplierCode);
    insertPurchase.setSupplierName(supplierDTO.getSupplierName());
    insertPurchase.setPurchaseSt(0);
    insertPurchase.setComments(purchaseEasyExcel.getComments());
    purchaseService.save(insertPurchase);
    purchase = insertPurchase;
}

    private void saveDetail() {
        for (PurchaseEasyExcel excel : cache) {
            if (StrUtil.isEmpty(excel.getSpuNo())) {
                throw new RootException("器材代码不可为空！");
            }
            if (excel.getPlanQty().compareTo(BigDecimal.ZERO) <= 0) {
                throw new RootException("收货数量必须大于0！");
            }
            PurchaseDetailDTO insertDetail = new PurchaseDetailDTO();
            PurchaseEntity purchaseEntity = purchaseService.findByDriverNo(purchase.getDeliverNo());
            insertDetail.setPurchaseId(purchaseEntity.getPurchaseId());
            insertDetail.setDeliverNo(purchase.getDeliverNo());
            String spuNo = excel.getSpuNo();
            SpuEntity spuEntity = spuService.findBySpuNo(spuNo);
            if (ObjectUtils.isEmpty(spuEntity)) {
                throw new RootException("请添加器材信息：" + spuNo);
            }
            insertDetail.setSpuNo(spuNo);
            insertDetail.setSpuName(spuEntity.getSpuName());
            insertDetail.setPlanQty(excel.getPlanQty());
            insertDetail.setDetailSt(0);
            insertDetail.setComments(COMMENT);
            purchaseDetailService.save(insertDetail);
        }
        cache.clear();
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        saveDetail();
        cache.clear();
        logger.info("入库单处理完成 count={}", total);
    }
}
